package jdbc.jdbc_Text.JDBC_pojo_Demo3;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.druid.pool.DruidPooledConnection;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.time.Period;
import java.util.ArrayList;
import java.util.Properties;
import java.util.Scanner;

/**
 * 综合练习:连接mysql,对品牌数据的增删改查
 * 1.准备环境
 * 2.数据库表tb_brand
 * 3.实体类Brand
 * 4.测试BrandText
 */
public class BrandText {
    static Scanner scanner=new Scanner(System.in);
    static ArrayList<Brand> list=null;
    public static void main(String[] args) throws Exception {
        Connection connection = DriverManager.getConnection("jdbc:mysql:///学生库?useSSL=false", "root", "mysql");

        /*
        连接池:
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///学生库?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("mysql");
        Connection connection = dataSource.getConnection();*/
        //获取数据库连接

       //显示操作
        // SelectAll(connection);
        //添加操作
        /*boolean b = InsertAdd(connection);
        if (b){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }*/
        //更改操作
        /*boolean update = Update(connection);
        if (update){
            System.out.println("更改成功");
        }else {
            System.out.println("更改失败");
        }*/
        //删除操作
        boolean delete = Delete(connection);
        if (delete){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    /**
     * 考虑思路:查询
     * 连接数据库:直接连接或用连接池
     * 1.sql语句:select * from tb_brand;
     * 2.参数:connection
     * 3.返回值:void
     * 4.结果:把查询的结构封装为对象在放入集合中(list(Brand))
     */
    public static void SelectAll(Connection connection) throws Exception {
        /*使用连接池步骤
        * 1.导包
        * 2.定义配置文件
        * 3.加载配置文件
        * 4.获取连接池对象
        * 5.获取数据库连接 Connection*/
        /*Properties prop=new Properties();
        prop.load(new FileInputStream("D:\\idea2022\\day.com.qiezi\\day_12\\druid.properties"));
        //获取连接池对象
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        //获取数据库连接
        Connection connection = dataSource.getConnection();
        System.out.println(connection);*/

        //定义sql语句
        String sql="select * from tb_brand";
        //获取sql执行对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //执行sql语句
        ResultSet resultSet = preparedStatement.executeQuery();
        //遍历执行后的数据,进行包装,再创建集合
        list=new ArrayList<>();
        while (resultSet.next()){
            int id = resultSet.getInt("id");
            String brandName = resultSet.getString("brand_name");
            String companyName = resultSet.getString("company_name");
            int ordered = resultSet.getInt("ordered");
            String description = resultSet.getString("description");
            int status = resultSet.getInt("STATUS");
            //创建对象
            Brand brand=new Brand();
            //添加对象的值
            brand.setId(id);
            brand.setBrandName(brandName);
            brand.setCompanyName(companyName);
            brand.setOrdered(ordered);
            brand.setDescription(description);
            brand.setStatus(status);
            //装载集合
            list.add(brand);
        }
        System.out.println(list);
        //释放资源
        resultSet.close();
        preparedStatement.close();
        connection.close();
    }
    /**
     * 添加功能:
     * 思路分析:因为不需要id,id是数据库自动根据生成
     * 1.sql语句:insert into tb_brand(brand_name,company_name,ordered,description,STATUS) values ('','','');
     * 2.参数:connection
     * 3.返回值:boolean
     * 4.结果:把用户输入的数据添加到数据库中
     */
    public static boolean InsertAdd(Connection connection) throws Exception {
        System.out.println("请你输入品牌名称:");
        String brandName=scanner.next();
        System.out.println("请你输入企业名称:");
        String companyName=scanner.next();
        System.out.println("请你输入排序字段:");
        int ordered=scanner.nextInt();
        System.out.println("请你输入描述信息:");
        String description=scanner.next();
        System.out.println("请你输入是否开启状态(0/1):");
        int status=scanner.nextInt();
        //定义sql语句
        String sql="insert into tb_brand(brand_name,company_name,ordered,description,STATUS) values (?,?,?,?,?);";
       //获取执行对象
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //添加参数
        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);
        //执行语句
        int i = preparedStatement.executeUpdate();
        //释放资源
        preparedStatement.close();
        connection.close();
        //处理结果
        if (i==0){
            return false;
        }else {
            return true;
        }
    }

    /**
     * 根据id修改数据
     * 思路分析:
     * 1.sql语句:Update tb_brand set brand_name=?,company_name=?,ordered=?,description=?,STATUS=? where id=?;
     * 2.参数connection
     * 3.返回值:boolean
     * 4.结果:把数据那出来,通过用户输入进行修改;
     * @param connection
     * @return
     */
    public static boolean Update(Connection connection) throws Exception {
        System.out.println("请你输入要修改的id:");
        int id = scanner.nextInt();
        System.out.println("请你输入品牌名称:");
        String brandName=scanner.next();
        System.out.println("请你输入企业名称:");
        String companyName=scanner.next();
        System.out.println("请你输入排序字段:");
        int ordered=scanner.nextInt();
        System.out.println("请你输入描述信息:");
        String description=scanner.next();
        System.out.println("请你输入是否开启状态(0/1):");
        int status=scanner.nextInt();
        String sql="Update tb_brand set brand_name=?," +
                "company_name=?,ordered=?,description=?,STATUS=? where id=?;";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //添加参数
        preparedStatement.setInt(6,id);
        preparedStatement.setString(1,brandName);
        preparedStatement.setString(2,companyName);
        preparedStatement.setInt(3,ordered);
        preparedStatement.setString(4,description);
        preparedStatement.setInt(5,status);
        int i = preparedStatement.executeUpdate();
        if (i==0){
            return false;
        }else {
            return true;
        }
    }
    /**
     * 根据id删除数据
     * 思路分析:
     * 1.sql语句:delete from tb_brand where id=?;
     * 2.参数connection
     * 3.返回值:boolean
     * 4.结果:把id进行删除
     * @param connection
     * @return
     */
    public static boolean Delete(Connection connection) throws Exception {
        System.out.println("请你输入要删除的id:");
        int id = scanner.nextInt();
        String sql="delete from tb_brand where id=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //添加参数
        preparedStatement.setInt(1,id);
        int i = preparedStatement.executeUpdate();
        if (i==0){
            return false;
        }else {
            return true;
        }
    }
}